#ifndef FONTSETTER_H
#define FONTSETTER_H

#include <QWidget>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QListWidget>
#include <QRegExpValidator>
#include <QFontDatabase>
#include <QMessageBox>
#include <QCheckBox>
#include <QCompleter>
#include <QApplication>

class FontSetter : public QWidget
{
    Q_OBJECT
signals:
    void sendFontOptsAndExit();
    void sendFontOpts();
    void exitOnly();
private:
    // Components
    QWidget* firstLineWidget;
    QWidget* fontFamilyPicker;
    QWidget* fontWeightPicker;
    QWidget* fontSizePicker;
    QWidget* bottomWidget;

    QLabel* fontFamilyLabel;
    QLineEdit* fontFamilyInput;
    QListWidget* fontFamilyList;

    QLabel* fontWeightLabel;
    QLineEdit* fontWeightInput;
    QListWidget* fontWeightList;

    QLabel* fontSizeLabel;
    QLineEdit* fontSizeInput;
    QListWidget* fontSizeList;

    QPushButton* confirmButton;
    QPushButton* applyButton;
    QPushButton* defaultFontButton;
    QPushButton* cancelButton;

    QLabel* useItalicLabel;
    QCheckBox* useItalicCheckBox;

    // Layout
    QVBoxLayout* fontFamilyLayout;
    QVBoxLayout* fontWeightLayout;
    QVBoxLayout* fontSizeLayout;
    QHBoxLayout* firstLineLayout;

    QHBoxLayout* secondLineLayout;

    QHBoxLayout* bottomLayout;
    QVBoxLayout* mainLayout;

    // Fun
    QRegExp fontSizeRegExp;
    QFontDatabase fontDatabase;
    QStringList fontFamilies;
    QStringList fontWeightStringList;
    QStringList fontSizeStringList;
    QCompleter* fontFamilyCompleter;
    QCompleter* fontWeightCompleter;
    QCompleter* fontSizeCompleter;

    const char* defaultFontFamily;
    int defaultFontSize;
    QFont::Weight defaultFontWeight;
    bool defaultUseItalic;
    QFont defaultItemFont;

    std::string* fontFamily;
    int* fontSize;
    QFont::Weight* fontWeight;
    bool* useItalic;

    void useDefaultOpts();
    void checkInput();
    void normalStatus();
    void changeItemsFont();
    void initLists();
    void updateFontFamilyInput();
    void updateFontWeightInput();
    void updateFontSizeInput();
    bool setNewFont(bool isCalled=false);
    void setNewFontAndExit();
    void changeUseItalic();
public:
    explicit FontSetter(QWidget *parent = nullptr);

    static const char* fontWeight2Str(const QFont::Weight fontWeight);
    static QFont::Weight str2FontWeight(std::string str);

    void getFontOpts(std::string& fontFamily, int& fontSize, QFont::Weight& fontWeight, bool& useItalic);
    void getDefaultFontOpts(const char* fontFamily, const int& fontSize, const QFont::Weight& fontWeight, const bool& useItalic);

    // UI Functions

    ~FontSetter();
};

#endif // FONTSETTER_H
